/*
@Description  用来获取配置参数的包
@Author  itcp  2021-02-06T09:32:54Z
@Update  itcp  2021-02-06T09:32:54Z
*/
package setting

import (
	"github.com/BurntSushi/toml"
	"time"
)
type Httpconf struct {
	Addr         int	   `toml:"addr"`
	ReadTimeout  time.Duration       `toml:"read_timeout"`
	WriteTimeout time.Duration       `toml:"write_timeout"`
	MaxHeaderBytes int     `toml:"max_header_bytes"`
	AllowIp      []string  `toml:"allow_ip"`
}
// 基本配置参数项
type BaseConf struct {
	Debug        string    
	TimeLocation string    `toml:"time_location"`
	Http         Httpconf  `toml:"http"`
}

// 连接MySQL的配置
type MySQLConf struct {
	Host      string
	Port        string
	Dbname      string 
	Username    string 
	Password    string
	ConnMax     int     `toml:"connection_max"`
	Enabled     bool
}

// 连接Redis的配置
type RedisConf struct {
    Master   Redis
	Slave    Redis
}

type Redis struct {
	ProxyList string `toml:"proxy_list"`
	Username  string
	Password  string
}

type InitConf struct {
	Env string
}

// 初始配置参数，选择环境
func GetInitConf() InitConf {
	var initMap InitConf
	if _, err := toml.DecodeFile("./conf/init.toml", &initMap);err != nil{
		panic(err)
	}
	return initMap
}
var ENV  = GetInitConf().Env

// 读取最基本应用信息
func GetBaseConf() BaseConf {
	var baseMap BaseConf
	if _, err := toml.DecodeFile("./conf/" + ENV + "/base.toml", &baseMap);err != nil{
		panic(err)
	}
	baseMap.Http.ReadTimeout = baseMap.Http.ReadTimeout  * time.Second
	baseMap.Http.WriteTimeout = baseMap.Http.WriteTimeout  * time.Second
	return baseMap
}

// 获取mysql连接参数
func GetMysqlConf() MySQLConf {
	var mysqlMap MySQLConf
	if _, err := toml.DecodeFile("./conf/" + ENV + "/mysql.toml", &mysqlMap);err != nil{
		panic(err)
	}
	return mysqlMap
}

// 获取redis连接参数
func GetRedisConf() RedisConf {
	var redisMap RedisConf
	if _, err := toml.DecodeFile("./conf/" + ENV + "/redis.toml", &redisMap);err != nil{
		panic(err)
	}
	return redisMap
}

var BaseSetting BaseConf
var MysqlSetting  MySQLConf
var RedisSetting  RedisConf

func init(){
	BaseSetting = GetBaseConf()
	MysqlSetting = GetMysqlConf()
	RedisSetting = GetRedisConf()
}